# author: alex
# time: 2022/4/25 20:28
"""
    注册测试
"""
import ddt
from selenium import webdriver
import time
from selenium.webdriver.common.by import By
import pandas as pd
import unittest

def readFile():
    file = pd.read_csv('data/register_data.csv')
    df = pd.DataFrame(file).fillna('')
    da=df.to_dict(orient='records')
    return da

@ddt.ddt
class Register_test(unittest.TestCase):

    def setUp(self) -> None:
        option = webdriver.ChromeOptions()
        option.binary_location = r'E:\Program Files\Google\Chrome\Application\chrome.exe'
        self.driver = webdriver.Chrome(options=option)
        self.url = 'http://localhost:8082/log-reg'
        self.driver.implicitly_wait(10)
        self.driver.get(self.url)
        self.driver.maximize_window()

    @ddt.data(*readFile())
    def test_login(self,data):
        print(data)
        self.driver.find_element(By.CSS_SELECTOR,
                        '#Log-Reg > div > div.container_overlay > div > div.overlay_panel.overlay_right'). \
            find_element(By.XPATH, "//button[@id='signup']").click()
        time.sleep(1)
        username = self.driver.find_element(By.CSS_SELECTOR, '#form1 > span:nth-child(2) > input')
        password = self.driver.find_element(By.CSS_SELECTOR, '#form1 > span:nth-child(3) > input')
        email = self.driver.find_element(By.CSS_SELECTOR, '#form1 > span:nth-child(4) > input')
        phoneNumber = self.driver.find_element(By.CSS_SELECTOR, '#form1 > span:nth-child(5) > input')

        username.send_keys(data.get('username'))
        password.send_keys(data.get('password'))
        email.send_keys(data.get('email'))
        phoneNumber.send_keys(data.get('phonenumber'))

        time.sleep(1)
        btn = self.driver.find_element(By.CSS_SELECTOR, '#form1 > button')
        # 模拟点击事件，提交数据
        btn.click()
        time.sleep(3)
        # body > div.el-overlay.is-message-box > div > div.el-message-box__content > div.el-message-box__container > div > p
        # body > div.el-overlay.is-message-box > div > div.el-message-box__content > div.el-message-box__container > div > p
        result=self.driver.find_element(By.CSS_SELECTOR,'body > div.el-overlay.is-message-box > div > div.el-message-box__content > div.el-message-box__container > div > p').text
        if data.get('state') == 1:
            self.assertEqual(result,'注册成功')
        else:
            self.assertNotEqual(result,'注册成功')


    def tearDown(self) -> None:
        self.driver.close()

if __name__=='__main__':
    unittest.main()



